package org.jiamandan.com.strategy;


import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jiamandan.com.dto.DXBasicDto;
import org.jiamandan.com.strategy.enums.DXEventTagEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


@Service
@Slf4j
public class DXContextService {

    @Autowired
    private final Map<String, IDXContextListener> strategyMap = new ConcurrentHashMap<>();

    public DXContextService(Map<String, IDXContextListener> strategyMap) {
        this.strategyMap.clear();
        strategyMap.forEach((k, v) -> this.strategyMap.put(k, v));
    }

    public void execute(DXBasicDto dto, Channel client) throws Exception {
//        String cmd = Integer.parseInt(dto.getFrameType(), 16) + "";
        String className = DXEventTagEnum.getValueByKey(dto.getCmd());
       // log.info("接收到嘉满电-DX报文，CMD=" + dto.getCmd());
        if (StringUtils.isNotBlank(className)) {
            strategyMap.get(className).doExecute(dto, client);
        }
    }
}
